package com.mindmax.user.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.mindmax.user.TreeSelect;
import com.mindmax.user.domain.Resource;
import com.mindmax.user.mapper.ResourceMapper;
import com.mindmax.user.service.IResourceService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 亿图权限点Service业务层处理
 *
 * @author 胡瑞鹏
 * @date 2024-04-22
 */
@Service
public class ResourceServiceImpl implements IResourceService
{
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 查询亿图权限点
     *
     * @param id 亿图权限点主键
     * @return 亿图权限点
     */
    @Override
    public Resource selectResourceById(Long id)
    {
        return resourceMapper.selectResourceById(id);
    }

    /**
     * 查询亿图权限点列表
     *
     * @param resource 亿图权限点
     * @return 亿图权限点
     */
    @Override
    public List<Resource> selectResourceList(Resource resource)
    {
        return resourceMapper.selectResourceList(resource);
    }

    /**
     * 新增亿图权限点
     *
     * @param resource 亿图权限点
     * @return 结果
     */
    @Override
    public int insertResource(Resource resource)
    {
        return resourceMapper.insertResource(resource);
    }

    /**
     * 修改亿图权限点
     *
     * @param resource 亿图权限点
     * @return 结果
     */
    @Override
    public int updateResource(Resource resource)
    {
        return resourceMapper.updateResource(resource);
    }

    /**
     * 批量删除亿图权限点
     *
     * @param ids 需要删除的亿图权限点主键
     * @return 结果
     */
    @Override
    public int deleteResourceByIds(Long[] ids)
    {
        return resourceMapper.deleteResourceByIds(ids);
    }

    /**
     * 删除亿图权限点信息
     *
     * @param id 亿图权限点主键
     * @return 结果
     */
    @Override
    public int deleteResourceById(Long id)
    {
        return resourceMapper.deleteResourceById(id);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<Resource> menus) {
        List<Resource> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Resource> selectResourceListByTenantId(Long tenantId) {
        return resourceMapper.selectResourceListByTenantId(tenantId);
    }


    public List<Resource> buildMenuTree(List<Resource> menus)
    {
        List<Resource> returnList = new ArrayList<Resource>();
        List<Long> tempList = menus.stream().map(Resource::getId).collect(Collectors.toList());
        for (Iterator<Resource> iterator = menus.iterator(); iterator.hasNext();)
        {
            Resource menu = (Resource) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }


    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t 子节点
     */
    private void recursionFn(List<Resource> list, Resource t)
    {
        // 得到子节点列表
        List<Resource> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Resource tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    private boolean hasChild(List<Resource> list, Resource t)
    {
        return !getChildList(list, t).isEmpty();
    }

    /**
     * 得到子节点列表
     */
    private List<Resource> getChildList(List<Resource> list, Resource t)
    {
        List<Resource> tlist = new ArrayList<Resource>();
        Iterator<Resource> it = list.iterator();
        while (it.hasNext())
        {
            Resource n = (Resource) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

}
